package com.wjw.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjw.pojo.Book;
import com.wjw.pojo.Reader;
import com.wjw.pojo.ReaderBook;
import com.wjw.service.BookSersive;
import com.wjw.service.ReaderBookService;
import com.wjw.service.ReaderService;
import com.wjw.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("readerBook")
public class ReaderBookController {

    @Autowired
    private ReaderBookService readerBookService;
    @Autowired
    private ReaderService readerService;
    @Autowired
    private BookSersive bookService;

    @RequestMapping("/listReaderBook") //分页,模糊查询
    public String list(@RequestParam(required = false,value = "pageNum",defaultValue = "1")Integer pageNum,
                       @RequestParam(required = false,value = "pageSize",defaultValue = "10")Integer pageSize, String readerName,
                        Model model) {
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ReaderBook>pageInfo=readerBookService.listAll(readerName);
        model.addAttribute("pageInfo",pageInfo);
        return "borrowList";
    }

    //跳转到借书页面,获取读者与书籍列表,供下拉选择
    @RequestMapping("preBorrow")
    public String preBorrow(Model model) {
        List<Reader> readerList = readerService.list(null);
        List<Book> bookList = bookService.list(null);
        model.addAttribute("readerList",readerList);
        model.addAttribute("bookList",bookList);
        return "borrowSave";
    }

    //借阅图书
    @RequestMapping("saveReaderBook")
    public String borrow(Integer readerId,Integer bookId){
        ReaderBook readerBook = new ReaderBook();
        readerBook.setRid(readerId);
        readerBook.setBid(bookId);
        boolean save = readerBookService.save(readerBook);
        //借书后此书库存减一
        Book book = bookService.getById(bookId);
        book.setStock(book.getStock()-1);
        bookService.updateById(book);
        return "redirect:/readerBook/listReaderBook";
    }

    //批量还书
    @PostMapping("batchReturnReaderBook")
    @ResponseBody
    public String batchReturnReaderBook(String idList){
        String[]split= StrUtil.split(idList,",");
        List<ReaderBook>readerBookList=new ArrayList<>();
        for (String s : split) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                ReaderBook readerBook = new ReaderBook();
                readerBook.setId(i);
                readerBook.setStatus(1);
                readerBookList.add(readerBook);
                //做库存批量增加
                Book book = bookService.getById(readerBookService.getById(i).getBid());
                book.setStock(book.getStock()+1);
                bookService.updateById(book);
            }
        }
        boolean b = readerBookService.updateBatchById(readerBookList);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }

    //批量删除
    @PostMapping("batchDeleteReaderBook")
    @ResponseBody
    public String batchDeleteReaderBook(String idList){
        String[]split= StrUtil.split(idList,",");
        List<Integer> list=new ArrayList<>();

        for (String s : split) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = readerBookService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }

    //删除借阅记录
    @RequestMapping("delReaderBook/{id}")
    public String delReaderBook(@PathVariable("id") Integer id){
        boolean b = readerBookService.removeById(id);
        return "redirect:/readerBook/listReaderBook";
    }

    //发送邮件
    @RequestMapping("sendEmail/{email}/{bname}")
    public String sendEmail(@PathVariable("email")String email,@PathVariable("bname")String bname){
        MailUtils.sendMail(email,"请在规定时间内归还图书《"+bname+"》。"+"<a href='http://localhost:8088'>去还书</a>","图书馆邮件");
        return "redirect:/readerBook/listReaderBook";
    }

}
